import { editStatus } from './constant.js'
import { jx_bill_testUri } from '@/api'
// 公用方法抽离
export const commonProcess = {
  data() {
    return {
      dynamicHeight: 250,
      gridConfig: {
        data: [],
        selectRows: [],
        gridColumns: [],
        selectData: []
      },
      editConfig: {
        transModeList:[
          {"code":"1","name":"1-CIF"},
          {"code":"2","name":"2-CFR"},
          {"code":"3","name":"3-FOB"}
        ],
        otherMarkList:[
          {"code":"1","name":"1率"},
          {"code":"2","name":"2单价"},
          {"code":"3","name":"3总价"}
        ],
        insurMarkList: [
          {"code":"1","name":"1率"},
          {"code":"2","name":"2单价"},
          {"code":"3","name":"3总价"}
        ],
        feeMarkList: [
          {"code":"1","name":"1率"},
          {"code":"2","name":"2单价"},
          {"code":"3","name":"3总价"}
        ],
        dutySelfList: [
          {"code":"Y","name":"Y 是"},
          {"code":"N","name":"N 否"}
        ],
        confirmSpecialList: [
          {"code":"Y","name":"Y 是"},
          {"code":"N","name":"N 否"}
        ],
        confirmPriceList: [
          {"code":"Y","name":"Y 是"},
          {"code":"N","name":"N 否"}
        ],
        confirmRoyaltiesList: [
          {"code":"Y","name":"Y 是"},
          {"code":"N","name":"N 否"}
        ],
        status:'',
        billListNo:'',
        tradeMode:'',
        declareType:'',
        rotateApplyNo:'',
        gMark:'',
        emsNo:'',
        headId: '',
        editData: {},
        editStatus: editStatus.SHOW
      },
      pageParam: {
        page: 1,
        limit: 15,
        dataTotal: 0,
        sort: ''
      },
      tableLoading: false, // 是否显示查询条件
      showSearch: false, // 查询条件行数
      hasChildTabs: false, // (列表高度)偏移值
      OffsetHeight: 0
    }
  },
  methods: {
    handleSearchSubmit() {
      this.pageParam.page = 1
      this.getList()
    },
    pageChange(page) {
      this.pageParam.page = page
      this.getList()
    },
    pageSizeChange(pageSize) {
      this.pageParam.limit = pageSize
      if (this.pageParam.page === 1) {
        this.getList()
      }
    },
    handleShowSearch() {
      this.showSearch = !this.showSearch
      this.refreshDynamicHeight()
    },
    handleSelectionChange(rows) {
      if (rows) {
        this.gridConfig.selectRows = rows
      }
    },
    isNullOrEmpty(val) {
      if (val === null) {
        return true
      }
      if (typeof val === 'undefined') {
        return true
      }
      if (val instanceof Date) {
        return false
      }
      if (typeof val === 'object' && Object.key(val).length === 0) {
        return true
      }
      return String(val).trim().length === 0
    },
    // 判断是否勾选数据以及是单选还是多选
    checkRowSelected(opTitle, isSingle) {
      if (isSingle !== true) {
        isSingle = false
      }
      if (this.isNullOrEmpty(opTitle)) {
        opTitle = '操作'
      }
      if (this.gridConfig.selectRows.length === 0) {
        this.$Message.warning('未选择数据，请选择您要' + opTitle + '的数据!')
        return false
      }else if (isSingle === true && this.gridConfig.selectRows.length > 1) {
        this.$Message.warning('一次仅能' + opTitle + '一条数据!')
        return false
      } else {
        return true
      }
    },
    getSearchParams() {
      return Object.assign({}, (this.$refs.listSearch ? this.$refs.listSearch.searchParams : {}))
    },
    getSidList() {
      if (this.gridConfig.selectRows.length > 0) {
        //return this.gridConfig.selectRows.map(item => item.sid)
        return this.gridConfig.selectRows.map(item => item.sid ? item.sid: item.oid)
      }
    },
    doDelete(url) {
    //  删除
      if (this.checkRowSelected('删除', false)) {
        this.$Modal.confirm({
          title: '提醒',
          content: '确认删除所选项吗?',
          okText: '删除',
          cancelText: '取消',
          onOk: () => {
            let sids = this.getSidList()
            this.$http.delete(`${url}/${sids}`).then(res => {
              if (res.data.success) {
                this.$Message.success(res.data.message)
                for (let i=0;i<this.gridConfig.selectRows.length;i++){
                  this.$http.post(jx_bill_testUri.xwManageHeadUrl.dcBillHeadXwt.insertDcBillDeleteLog,
                    {
                      emsNo:this.gridConfig.selectRows[i].emsNo,
                      tradeCode:this.gridConfig.selectRows[i].tradeCode,
                      billListNo:this.gridConfig.selectRows[i].billListNo,
                      emsListNo:this.gridConfig.selectRows[i].emsListNo,
                      insertUser:this.gridConfig.selectRows[i].insertUser,
                      iEMark:this.gridConfig.selectRows[i].iEMark,
                      status:this.gridConfig.selectRows[i].status
                    }).then(res => {
                    console.log(res)
                  }, () => {});
                }
              }
            }, () => {}).finally(() => {
              this.gridConfig.selectRows = []
              this.getList()
            })
          }
        })
      }
    },
    getCurrentDateString () {
      let date = new Date()
      let year = date.getFullYear()
      let month = (date.getMonth() + 1).toString().padStart(2,'0')
      let day = date.getDate().toString().padStart(2,'0');
      return year + "" + month + "" + day
    },
    getAsyncExportColumns(columns){
      let columnsInfo = []
      if(Array.isArray(columns)){
        columns.forEach(item => {
          if(item.key && item.key !=='selection' && item.key !=='operation') {
            columnsInfo.push({ key: item.key, value: item.title })
          }
        })
      }
      return columnsInfo
    },
    refreshDynamicHeight () {
      this.$nextTick(() => {
        const refs = this.$refs
        let tabHeight = 42
        // 當存在子Tab組件時去除此子Tab頭高度: 38px
        let childTabHeight = 0
        if (this.hasChildTabs) {
          childTabHeight = 38
        }
        // 麵包屑标签行高度: 28px (包含查询按钮)
        let breadCrumbHeight = 28
        // 底部信息欄高度: 28px
        let bottomHeight = 28
        // 得出基礎高度
        let hiddenHeight = window.innerHeight - tabHeight - childTabHeight - breadCrumbHeight - bottomHeight
        // 去除按鈕行高度: 28px(需要添加2px: 与上下之間的空隙)
        if (refs.hasOwnProperty("area_actions")) {
          hiddenHeight = hiddenHeight - 30
        }
        // 去除分頁信息行高度: 28px
        if (refs.hasOwnProperty("area_page")) {
          hiddenHeight = hiddenHeight - 28
        }
        // 減去偏移量(某些特殊情況使用)
        hiddenHeight = hiddenHeight - this.OffsetHeight - 2
        // 當查詢條件塊打開時候去除其高度
        if (this.showSearch === true) {
          let searchHeight = refs["area_search"].getBoundingClientRect().height
          this.dynamicHeight = hiddenHeight - searchHeight -1
          return
        }
        this.dynamicHeight = hiddenHeight
      })
    }
  }
}
